package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.PartGroup;
import com.efast.dmom.mes.bean.PartGroupItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PartGroupDataHandler;
import com.efast.dmom.mes.datahandlers.entity.PartGroupItemDataHandler;
import com.efast.dmom.mes.entity.PartGroupEntity;
import com.efast.dmom.mes.entity.PartGroupItemEntity;
import com.efast.dmom.mes.service.PartGroupService;
import com.efast.dmom.mes.util.StringVerifyUtil;

@Service
public class PartGroupServiceImpl implements PartGroupService {
	
	@Autowired
	private PartGroupDataHandler PartGroupDataHandler;
	
	@Autowired
	private PartGroupItemDataHandler partGroupItemDataHandler; 

	@Override
	public ResultBean save(PartGroup partGroup) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartGroupEntity partGroupEntity = partGroup.getInnerData();
		if (partGroupEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(partGroupEntity.getPartGroupCode())
				&& !StringUtil.isNotBlank(partGroupEntity.getName())
				&& !StringUtil.isNotBlank(partGroupEntity.getPartGroupType())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组编号是否填写！");
             return resultBean;
		}
		if (!StringUtil.isNotBlank(partGroupEntity.getName())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组名称是否填写！");
             return resultBean;
		}	if (!StringUtil.isNotBlank(partGroupEntity.getPartGroupType())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组类型是否填写！");
             return resultBean;
		}
		if (partGroupEntity.getPlantId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工厂是否填写！");
			return resultBean;
		}
		PartGroup partGroupByCode = PartGroupDataHandler.getByPartGroupCode(partGroupEntity.getPartGroupCode());
		if (partGroupByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("物料组编号不唯一！");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(partGroupEntity.getPartGroupCode())){
		    resultBean.setSuccess(false);
		    resultBean.setMessage("物料组编号不是由英文、字母、下划线组成!!");
		    return resultBean;
		 }
		return PartGroupDataHandler.save(partGroup);
	}

	@Override
	public ResultBean update(PartGroup partGroup) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartGroupEntity partGroupEntity = partGroup.getInnerData();
		if (partGroupEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(partGroupEntity.getPartGroupCode())
				&& !StringUtil.isNotBlank(partGroupEntity.getName())
				&& !StringUtil.isNotBlank(partGroupEntity.getPartGroupType())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组编号是否填写！");
             return resultBean;
		}
		if (!StringUtil.isNotBlank(partGroupEntity.getName())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组名称是否填写！");
             return resultBean;
		}	if (!StringUtil.isNotBlank(partGroupEntity.getPartGroupType())) {
             resultBean.setSuccess(false);
             resultBean.setMessage("请检查物料组类型是否填写！");
             return resultBean;
		}
		if (partGroupEntity.getPlantId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工厂是否填写！");
			return resultBean;
		}
		PartGroup partGroupById = PartGroupDataHandler.getById(partGroupEntity.getId());
		if (!partGroupById.getInnerData().getPartGroupCode().equals(partGroupEntity.getPartGroupCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("物料编号不可修改！");
			return resultBean;
		}
		return PartGroupDataHandler.update(partGroup);
	}

	@Override
	public ResultBean delete(PartGroup partGroup) {
		ResultBean resultBean = new ResultBean(true,' ');
		List<PartGroupItem> itemList = partGroupItemDataHandler.getByPartGroupId(partGroup.getInnerData().getId());
		if (itemList != null && itemList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setObject(partGroup);
			resultBean.setMessage("该物料组下存在物料子项!");
			return resultBean;
		}
		return PartGroupDataHandler.delete(partGroup);
	}
	
	@Override
	public ResultBean deleteAll(PartGroup partGroup) {
		ResultBean resultBean = new ResultBean(true,' ');
		PartGroupDataHandler.delete(partGroup);
		PartGroupItem partGroupItem = new PartGroupItem();
		PartGroupItemEntity partGroupItemEntity = new PartGroupItemEntity();
		partGroupItemEntity.setPartGroupId(partGroup.getInnerData().getId());
		partGroupItem.setInnerData(partGroupItemEntity);
		partGroupItemDataHandler.deleteByPartGroupId(partGroupItem);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	@Override
	public PartGroup getById(long id) {
		return PartGroupDataHandler.getById(id);
	}

	@Override
	public PartGroup getByName(String name) {
		return PartGroupDataHandler.getByName(name);
	}

}
